package com.rtx.edi.platform.synctask.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.rtx.api.annotation.RtxActivegatway;
import com.rtx.edi.model.api.bo.ApiMappingInfoBo;
import com.rtx.edi.platform.synctask.bean.bo.ExternLogicBo;
import com.rtx.edi.platform.synctask.service.GatWayService;
import com.rtx.wms.customer.grp.handler.fs.SoAndAsnFSHandle;

import cn.hutool.http.HttpRequest;

@RtxActivegatway("格瑞普ERP接口")
@Service("GrpGatWayService")
public class GrpApiGatWayServiceImpl extends GatWayService{
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	ApplicationContext applicationContext;
	
	
	@Override
	protected String handleTarget(String targetjson, ApiMappingInfoBo apiInfo) {
		logger.info("目标请求地址：{}",apiInfo.getTargetUrl());
		
		//设置请求报文
		apiInfo.getContent().setRequestContent(targetjson);
		
		String	result = HttpRequest
				.post(apiInfo.getTargetUrl())
				.body(targetjson)
				.timeout(30000)
				.execute()
				.body();
		//设置响应报文
		apiInfo.getContent().setResponseContent(result);
		
		//转发后执行逻辑
		handleResponseExternExternLogic(targetjson,result,apiInfo);
		
		return result;
	}
	
	//转发后执行逻辑
	private void handleResponseExternExternLogic(String request,String	result, ApiMappingInfoBo apiInfo) {
		ExternLogicBo bo = new ExternLogicBo();
		bo.setRequest(request);
		bo.setResponse(result);
		bo.setMappingConfig(apiInfo);
		try {
			SoAndAsnFSHandle handle = (SoAndAsnFSHandle) applicationContext.getBean(apiInfo.getApimappingcode()+"-response");
			handle.handle(bo);
		} catch (Exception e) {
			logger.error("转发后执行逻辑异常",e);
		}

	}
}
